import Promise from 'bluebird';

import service from '../service/message';
import CONSTANT from '../constant/message';

const getCustomerMessageList = ({ commit }, page) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getCustomerMessageList(page).then((res) => {
      commit(CONSTANT.MESSAGE_GET_CUSTOMER_LIST_SUCCESS, res);
      return resolve(res.publicPushesResponse);
    }, (e) => {
      commit(CONSTANT.MESSAGE_GET_CUSTOMER_LIST_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const getTemplateMessageList = ({ commit }, page) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getTemplateMessageList(page).then((res) => {
      commit(CONSTANT.MESSAGE_GET_TEMPLATE_LIST_SUCCESS, res);
      return resolve(res.templatePushesResponse);
    }, (e) => {
      commit(CONSTANT.MESSAGE_GET_TEMPLATE_LIST_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const getCustomerMessage = ({ commit }, id) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getCustomerMessage(id).then((res) => {
      commit(CONSTANT.MESSAGE_GET_CUSTOMER_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.MESSAGE_GET_CUSTOMER_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const addCustomerMessage = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.addCustomerMessage(info).then((res) => {
      commit(CONSTANT.MESSAGE_ADD_CUSTOMER_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.MESSAGE_ADD_CUSTOMER_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const addTemplateMessage = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.addTemplateMessage(info).then((res) => {
      commit(CONSTANT.MESSAGE_ADD_TEMPLATE_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.MESSAGE_ADD_TEMPLATE_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const updateCustomerMessage = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.updateCustomerMessage(info).then((res) => {
      commit(CONSTANT.MESSAGE_UPDATE_CUSTOMER_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.MESSAGE_UPDATE_CUSTOMER_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const updateTemplateMessage = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.updateTemplateMessage(info).then((res) => {
      commit(CONSTANT.MESSAGE_UPDATE_TEMPLATE_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.MESSAGE_UPDATE_TEMPLATE_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const getSubscribeMessageList = ({ commit }, page) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getSubscribeMessageList(page).then((res) => {
      commit(CONSTANT.MESSAGE_GET_SUBSCRIBE_LIST_SUCCESS, res);
      return resolve(res.list);
    }, (e) => {
      commit(CONSTANT.MESSAGE_GET_SUBSCRIBE_LIST_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const getSubscribeMessage = ({ commit }, id) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getSubscribeMessage(id).then((res) => {
      commit(CONSTANT.MESSAGE_GET_SUBSCRIBE_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.MESSAGE_GET_SUBSCRIBE_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const addSubscribeMessage = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.addSubscribeMessage(info).then((res) => {
      commit(CONSTANT.MESSAGE_ADD_SUBSCRIBE_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.MESSAGE_ADD_SUBSCRIBE_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const updateSubscribeMessage = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.updateSubscribeMessage(info).then((res) => {
      commit(CONSTANT.MESSAGE_UPDATE_SUBSCRIBE_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.MESSAGE_UPDATE_SUBSCRIBE_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const deleteSubscribeMessage = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.deleteSubscribeMessage(info).then((res) => {
      commit(CONSTANT.MESSAGE_DELETE_SUBSCRIBE_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.MESSAGE_DELETE_SUBSCRIBE_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const uploadMpNewsMessage = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.uploadMpNewsMessage(info).then((data) => {
      commit(CONSTANT.MESSAGE_UPLOAD_MPNEWS_SUCCESS, data);
      return resolve(data);
    }, (e) => {
      commit(CONSTANT.MESSAGE_UPLOAD_MPNEWS_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const deleteCustomerMessage = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.deleteCustomerMessage(info).then((res) => {
      commit(CONSTANT.MESSAGE_DELETE_CUSTOMER_SUCCESS, res);
      return resolve(res);
    }, (e) => {
      commit(CONSTANT.MESSAGE_DELETE_CUSTOMER_ERROR, e);
      return reject(e);
    });
    return server;
  });
  return ret;
};

const deleteTemplateMessage = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.deleteTemplateMessage(info).then((res) => {
      return resolve(res);
    }, (e) => {
      return reject(e);
    });
    return server;
  });
  return ret;
};


const previewMessage = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.previewMessage(info).then((res) => {
      commit(CONSTANT.MESSAGE_PREVIEW_SUCCESS);
      resolve(res);
    }, e => reject(e));
    return server;
  });
  return ret;
};
const previewTemplateMessage = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.previewTemplateMessage(info).then((res) => {
      resolve(res);
    }, e => reject(e));
    return server;
  });
  return ret;
};
const get48openIds = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.get48openIds(info).then((res) => {
      commit(CONSTANT.MESSAGE_GET_48OPENID_SUCCESS, res.total);
      resolve(res.total);
    }, e => reject(e));
    return server;
  });
  return ret;
};
const getMpNewsByMediaId = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getMpNewsByMediaId(info).then((res) => {
      commit(CONSTANT.MESSAGE_GET_MPNEWS_SUCCESS, res);
      resolve(res);
    }, e => reject(e));
    return server;
  });
  return ret;
};
const getImageByMediaId = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getImageByMediaId(info).then((res) => {
      commit(CONSTANT.MESSAGE_GET_IMAGE_SUCCESS, res);
      resolve(res);
    }, e => reject(e));
    return server;
  });
  return ret;
};
const getVoiceByMediaId = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.getVoiceByMediaId(info).then((res) => {
      commit(CONSTANT.MESSAGE_GET_VOICE_SUCCESS, res);
      resolve(res);
    }, e => reject(e));
    return server;
  });
  return ret;
};

const changeSubscribeMessageStatus = ({ commit }, info) => {
  const ret = new Promise((resolve, reject) => {
    const server = service.changeSubscribeMessageStatus(info).then((res) => {
      commit(CONSTANT.MESSAGE_CHANGE_SUBSRIBE_MESSAGE_SUCCESS);
      resolve(res);
    }, e => reject(e));
    return server;
  });
  return ret;
};

export default {
  getTemplateMessageList,
  addTemplateMessage,
  updateTemplateMessage,
  deleteTemplateMessage,
  getCustomerMessageList,
  getCustomerMessage,
  addCustomerMessage,
  updateCustomerMessage,
  deleteCustomerMessage,
  getSubscribeMessageList,
  getSubscribeMessage,
  addSubscribeMessage,
  updateSubscribeMessage,
  deleteSubscribeMessage,
  previewMessage,
  previewTemplateMessage,
  uploadMpNewsMessage,
  get48openIds,
  getMpNewsByMediaId,
  getImageByMediaId,
  getVoiceByMediaId,
  changeSubscribeMessageStatus,
};
